﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Data.SqlTypes;
using System.Text;
using System.Data;

namespace FreeProperty.Views.ProviderDirectory
{
    public partial class Test : System.Web.UI.Page
    {
        LatLng oLatLng = new LatLng();
        string sAddr = "new delhi";
        string API_KEY = "ABQIAAAAIApAPCSt0YcKoeWffEil-hS-o7DPUZlmwEMlmzAhmXOR1uAo2RSfjoONZnsMPwn4y0IB9AqL4AVjtg";
        protected int PageNumber = 1;
        //protected DataView _dVData = null;

        protected SqlInt32 _totalPages = SqlInt32.Null;
        protected SqlInt32 _totalItems = SqlInt32.Null;
        public SqlInt32 TotalPages
        {
            get { return _totalPages; }
            set
            {
                _totalPages = value;
            }
        }
        public SqlInt32 TotalItems
        {
            get { return _totalItems; }
            set
            {
                _totalItems = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (hdnMode.Value == "GMAP")
            {
                var url = "http://maps.google.co.in/maps?f=q&source=s_q&hl=en&geocode=&q=" + HttpContext.Current.Server.UrlEncode(txtAddress.Text);
                gmapURL.HRef = url;

                
            }
            if (hdnMode.Value == "")
            {
                gvData.AutoGenerateColumns = true;

                List<SubjectData> lstCompleteData = GetSubjectList();
                hdnCurrentPage.Value = "1";
                hdnPageCount.Value = PagingUtil.GetPagesCount(lstCompleteData.Count).ToString();
                BindPagingData();
            }
            if (hdnMode.Value == "DATA")
            {

                gvData.AutoGenerateColumns = true;
                hdnCurrentPage.Value = "1";
                hdnPageCount.Value = "0";
                BindPagingData();
            }
            switch (hdnStatus.Value.ToUpper())
            {

                case "PAGING":
                    Paging();
                    break;
                case "PREVIOUS":
                    Previous();
                    break;
                case "NEXT":
                    Next();
                    break;
                case "FIRST":
                    First();
                    break;
                case "LAST":
                    Last();
                    break;


            }
        }
        protected void Page_Load1(object sender, EventArgs e)
        {
            //oLatLng = GetLatLng(sAddr);
            //string sLat = "28.635308";
            //string sLong = "77.22496";
            //if (tbLat.Value.Length > 0)
            //{
            //    sLat = tbLat.Value;
            //}
            //if (tbLong.Value.Length > 0)
            //{
            //    sLong = tbLong.Value;
            //}
            //frameGMap.Attributes["src"] = "http://maps.google.co.in/?ll=" + sLat + "," + sLong + "&output=embed";
            //tbLat.Value = sLat;
            //tbLong.Value = sLong;

            //var url = "http://maps.google.co.in/maps?f=q&source=s_q&hl=en&geocode=&q=" + HttpContext.Current.Server.UrlEncode("215 CENTRAL AVENUE #101 LOUISVILLE KY 40208");
            //gmapURL.HRef = url;



            //frameGMap.AppRelativeTemplateSourceDirectory = "http://maps.google.co.in/?ll=" + oLatLng.Latitude + "," + oLatLng.Longitude + "&output=embed";
            //gvData.ver
            //gvData.AutoGenerateColumns = true;
            //gvData.AllowPaging = true;
            //gvData.PageSize = 3;
            //gvData.DataSource = GetSubjectList();
            //gvData.DataBind();

        }
        public List<SubjectData> GetSubjectList()
        {
            var lst = new List<SubjectData>();
            lst.Add(new SubjectData { SubName = "a11", SubAge = 10, SubId = 1 });
            lst.Add(new SubjectData { SubName = "a12", SubAge = 11, SubId = 2 });
            lst.Add(new SubjectData { SubName = "a13", SubAge = 12, SubId = 3 });
            lst.Add(new SubjectData { SubName = "a14", SubAge = 13, SubId = 4 });
            lst.Add(new SubjectData { SubName = "a15", SubAge = 14, SubId = 5 });
            lst.Add(new SubjectData { SubName = "a16", SubAge = 15, SubId = 6 });
            lst.Add(new SubjectData { SubName = "a17", SubAge = 16, SubId = 7 });
            lst.Add(new SubjectData { SubName = "a18", SubAge = 17, SubId = 8 });
            lst.Add(new SubjectData { SubName = "a19", SubAge = 18, SubId = 9 });
            lst.Add(new SubjectData { SubName = "a10", SubAge = 19, SubId = 10 });

            return lst;
        }
        public LatLng GetLatLng(string addr)
        {
            var url = "http://maps.google.co.in/maps/geo?output=csv&key=" +

                       this.API_KEY + "&q=" + HttpContext.Current.Server.UrlEncode(addr);

            var request = WebRequest.Create(url);

            var response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {

                var ms = new MemoryStream();

                var responseStream = response.GetResponseStream();

                var buffer = new Byte[2048];

                int count = responseStream.Read(buffer, 0, buffer.Length);

                while (count > 0)
                {

                    ms.Write(buffer, 0, count);

                    count = responseStream.Read(buffer, 0, buffer.Length);

                }

                responseStream.Close();

                ms.Close();

                var responseBytes = ms.ToArray();

                var encoding = new System.Text.ASCIIEncoding();

                string coords = encoding.GetString(responseBytes);

                var parts = coords.Split(',');

                return new LatLng(

                              Convert.ToDouble(parts[2]),

                              Convert.ToDouble(parts[3]));

            }

            return null;

        }
        protected void BindPagingData()
        {
            int currentPage = Convert.ToInt32(hdnCurrentPage.Value);
            int startingIndex = (currentPage - 1) * PagingUtil.PageSize;
            int itemsToFetch = PagingUtil.PageSize;
            List<SubjectData> lstCompleteData = GetSubjectList();
            if ((startingIndex + itemsToFetch) > lstCompleteData.Count)
            {
                itemsToFetch = lstCompleteData.Count - startingIndex;
            }
            List<SubjectData> lst = lstCompleteData.GetRange(startingIndex, itemsToFetch);
            PagingUtil.BindDataToGrid(gvData,lst, "Views/ProviderDirectory/Test.aspx", "AD", spanFootServerPaging, hdnCurrentPage, hdnPageCount, lnkFootNext, lnkFootLast, lnkFootFirst, lnkFootPrevious);
        }
        private void DoPaging()
        {
            int startNumber = 0;
            int endNumber = 0;
            StringBuilder strPaging = new StringBuilder();
            int CurrentPage = Convert.ToInt32(hdnCurrentPage.Value) - 1;
            startNumber = CurrentPage - ((int)(CurrentPage % 10));

            lnkFootNext.Visible = true;
            lnkFootLast.Visible = true;
            lnkFootFirst.Visible = true;
            lnkFootPrevious.Visible = true;

            if (Convert.ToInt32(hdnCurrentPage.Value) == 1)
            {
                lnkFootFirst.Visible = false;
                lnkFootPrevious.Visible = false;
            }
            if (Convert.ToInt32(hdnCurrentPage.Value) == Convert.ToInt32(hdnPageCount.Value))
            {
                lnkFootNext.Visible = false;
                lnkFootLast.Visible = false;
            }
            if (Convert.ToInt32(hdnPageCount.Value) > (startNumber + 9))
                endNumber = startNumber + 9;
            else
                endNumber = Convert.ToInt32(hdnPageCount.Value) - 1;

            for (int i = startNumber; i <= endNumber; i++)
            {
                if (CurrentPage == i)
                    strPaging.Append("<b>" + ((i + 1) + "&nbsp;") + "</b>");
                else
                    strPaging.Append("<a href='#' style='cursor:hand' onclick=javascript:frmSearchCommandArgument('" + (i + 1) + "','Views/ProviderDirectory/Test.aspx','frmContent','AD');>" + ((i + 1) + "&nbsp;") + "</a>");

            }

            spanFootServerPaging.InnerHtml = strPaging.ToString();

            lnkFootPrevious.Attributes.Add("onclick", "javascript:frmSearchPaging('Previous','Views/ProviderDirectory/Test.aspx','frmContent','AD');");
            lnkFootNext.Attributes.Add("onclick", "javascript:frmSearchPaging('Next','Views/ProviderDirectory/Test.aspx','frmContent','AD');");
            lnkFootFirst.Attributes.Add("onclick", "javascript:frmSearchPaging('First','Views/ProviderDirectory/Test.aspx','frmContent','AD');");
            lnkFootLast.Attributes.Add("onclick", "javascript:frmSearchPaging('Last','Views/ProviderDirectory/Test.aspx','frmContent','AD');");
        }
       
        protected void Paging()
        {
            BindPagingData();

        }
        
        protected void Previous()
        {
            hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) - 1).ToString();
            BindPagingData();

        }
        
        protected void Next()
        {
            hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) + 1).ToString();
            BindPagingData();

        }
        
        protected void First()
        {
            hdnCurrentPage.Value = "1";
            BindPagingData();
        }
        
        protected void Last()
        {
            hdnCurrentPage.Value = hdnPageCount.Value;
            BindPagingData();
        }
    }
    public class LatLng
    {
        public double Latitude { get; set; }

        public double Longitude { get; set; }

        public LatLng()
        {

        }


        public LatLng(double lat, double lng)
        {
            this.Latitude = lat;

            this.Longitude = lng;

        }
    }
    public class SubjectData
    {
        private string subName;

        public string SubName
        {
            get { return subName; }
            set { subName = value; }
        }
        private int subId;

        public int SubId
        {
            get { return subId; }
            set { subId = value; }
        }
        private int subAge;

        public int SubAge
        {
            get { return subAge; }
            set { subAge = value; }
        }
    }
}